package com.mmall.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created by geely
 */
public class PropertiesUtil {

    //用了日志处理方法,放到日志文件中
    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static Properties props;

    //软件启动就加载配置文件
    static {
        try {

            String fromName = "mmall.properties";
            //定义一个文件读取类
            props = new Properties();
            //加载配置文件,用到io流,并且给定字符集
            props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fromName),"utf-8"));
        } catch (IOException e) {
             logger.error("配置文件读取失败",e);
        }
    }

    public static String getProperty(String key){
        //读取对应的value值
        //key.trim())表示忽略空格符  AA  AA  -->AAAA
        String value = props.getProperty(key.trim());
        //判断value是否为空
        if(StringUtils.isBlank(value)){
            return null;
        }
        return value.trim();//获的value去除空格
    }

    public static String getProperty(String key,String defaultValue){

        //读取对应的value值
        String value = props.getProperty(key.trim());
        //为了兜底,就是防止key没有值, 给定一个value值
        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value.trim();
    }
}
